package cn.haut.online_read.crawler;

import cn.haut.online_read.enrity.BookInfo;
import lombok.extern.slf4j.Slf4j;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.List;

/**
 * 根据url爬取页面的书籍信息，只爬6个
 * 因为微信阅读网页每个也只显示6个，更多的需要进入显示全部榜单的页面
 *
 * TODO 现在获取的不是6个而是所有的，一页会有 大概 84个
 *
 * @author xing
 * @date 2021-05-05 19:16
 */
@Slf4j
public class RankingListBookCrawler implements PageProcessor {

    private Site site = Site.me().setSleepTime(1);

    @Override
    public void process(Page page) {
        List<String> bookIdList = page.getHtml().xpath("//div[@class='books-list']/a/@href").replace("/book/", "").all();
        List<String> imgUrlList = page.getHtml().xpath("//div[@class='books-list']/a/img/@src").replace("/^/", "http:").all();
        List<String> bookNameList = page.getHtml().xpath("//div[@class='books-list']/a/div/h4[@class='name']/span/text()").all();
        List<String> authorList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='author']/span/text()").all();
        List<String> descList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='desc']/text()").all();
        List<String> popularityList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='popularity']/span[1]/text()").all();
        List<String> keepList = page.getHtml().xpath("//div[@class='books-list']/a/div/p[@class='popularity']/span[3]/text()").all();

        List<BookInfo> bookInfoList = BookInfo.getBookInfoList(bookIdList, bookNameList, authorList, descList, imgUrlList, keepList, popularityList);
        page.putField("bookInfos", bookInfoList);
    }

    @Override
    public Site getSite() {
        return site;
    }
}
